package com.changgou.comment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.changgou.comment.dao.CommentMapper;
import com.changgou.comment.pojo.Comment;
import com.changgou.comment.service.CommentService;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:Comment业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;


    @Autowired
    private SkuFeign skuFeign;


    /**
     * 根据Skuid查询评价
     *
     * @param skuid
     * @return
     */
    @Override
    public PageInfo<Map<String, Object>> findPageBySkuid(Long skuid, int page, int size) {
        //分页
        PageHelper.startPage(page, size);
        //根据skuid查询对应的sku
        Sku sku = skuFeign.findById(skuid).getData();
        Long spuId = sku.getSpuId();
        //根据spuid查询对应的sku集合
        Sku sku1 = new Sku();
        sku1.setSpuId(spuId);
        List<Sku> skuList = skuFeign.findList(sku1).getData();
        List<Long> skuids = new ArrayList<>();
        for (Sku sku2 : skuList) {
            skuids.add(sku2.getId());
        }

        //构建搜索条件
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("skuid", skuids);
        //执行搜索
        PageInfo<Comment> pageInfo = new PageInfo<Comment>(commentMapper.selectByExample(example));

        //将PageInfo<Comment>转换成PageInfo<Map<String, Object>>
        PageInfo<Map<String, Object>> pageInfo1 = new PageInfo<>();
        pageInfo1.setPageNum(pageInfo.getPageNum());
        pageInfo1.setPageSize(pageInfo.getPageSize());
        pageInfo1.setSize(pageInfo.getSize());
        pageInfo1.setStartRow(pageInfo.getStartRow());
        pageInfo1.setEndRow(pageInfo.getEndRow());
        pageInfo1.setTotal(pageInfo.getTotal());
        pageInfo1.setPages(pageInfo.getPages());
        pageInfo1.setPrePage(pageInfo.getPrePage());
        pageInfo1.setNextPage(pageInfo.getNextPage());

        int count = 0;
        List<Map<String, Object>> list = new ArrayList<>();
        for (Comment comment : pageInfo.getList()) {
            Map<String, Object> map = (HashMap<String, Object>) JSON.parseObject(JSON.toJSONString(comment), Map.class);
            String name = skuFeign.findById(comment.getSkuid()).getData().getName();
            map.put("skuName", name);
            Date date = comment.getCommentTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(date);
            map.put("commentTime", format);
            String goodStar = comment.getGoodStar();
            int i = 0;
            if (!StringUtils.isEmpty(goodStar) && !goodStar.equalsIgnoreCase("null")) {
                i = Integer.parseInt(goodStar);
            }
            if (i == 5) {
                count += 1;
            }
            List<String> list1 = new ArrayList<>();
            for (int n = 0; n < i; n++) {
                list1.add("aaa");
            }
            map.put("starList", list1);
            String imgStr = (String) map.get("commentPicture");
            String[] split = imgStr.split(";");
            map.put("commentPicture", split);
            list.add(map);
        }

        //计算好评度
        if (list != null && list.size() > 0) {
            int u = (count * 100 / list.size());
            pageInfo1.setNavigateLastPage(u);
        }
        pageInfo1.setList(list);
        return pageInfo1;
    }

    /**
     * Comment条件+分页查询
     *
     * @param comment 查询条件
     * @param page    页码
     * @param size    页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Comment> findPage(Comment comment, int page, int size) {
        //分页
        PageHelper.startPage(page, size);
        //搜索条件构建
        Example example = createExample(comment);
        //执行搜索
        return new PageInfo<Comment>(commentMapper.selectByExample(example));
    }

    /**
     * Comment分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Comment> findPage(int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        //分页查询
        return new PageInfo<Comment>(commentMapper.selectAll());
    }

    /**
     * Comment条件查询
     *
     * @param comment
     * @return
     */
    @Override
    public List<Comment> findList(Comment comment) {
        //构建查询条件
        Example example = createExample(comment);
        //根据构建的条件查询数据
        return commentMapper.selectByExample(example);
    }


    /**
     * Comment构建查询对象
     *
     * @param comment
     * @return
     */
    public Example createExample(Comment comment) {
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        if (comment != null) {
            // 
            if (!StringUtils.isEmpty(comment.getId())) {
                criteria.andEqualTo("id", comment.getId());
            }
            // 
            if (!StringUtils.isEmpty(comment.getSkuid())) {
                criteria.andEqualTo("skuid", comment.getSkuid());
            }
            // 
            if (!StringUtils.isEmpty(comment.getUsername())) {
                criteria.andLike("username", "%" + comment.getUsername() + "%");
            }
            // 
            if (!StringUtils.isEmpty(comment.getCommentTime())) {
                criteria.andEqualTo("commentTime", comment.getCommentTime());
            }
            // 
            if (!StringUtils.isEmpty(comment.getCommentPicture())) {
                criteria.andEqualTo("commentPicture", comment.getCommentPicture());
            }
            // 
            if (!StringUtils.isEmpty(comment.getTransportStar())) {
                criteria.andEqualTo("transportStar", comment.getTransportStar());
            }
            // 
            if (!StringUtils.isEmpty(comment.getGoodStar())) {
                criteria.andEqualTo("goodStar", comment.getGoodStar());
            }
            // 
            if (!StringUtils.isEmpty(comment.getFastStar())) {
                criteria.andEqualTo("fastStar", comment.getFastStar());
            }
            // 
            if (!StringUtils.isEmpty(comment.getPostmanStar())) {
                criteria.andEqualTo("postmanStar", comment.getPostmanStar());
            }
            // 
            if (!StringUtils.isEmpty(comment.getContent())) {
                criteria.andEqualTo("content", comment.getContent());
            }
            // 
            if (!StringUtils.isEmpty(comment.getHide())) {
                criteria.andEqualTo("hide", comment.getHide());
            }
            // 
            if (!StringUtils.isEmpty(comment.getNiceCount())) {
                criteria.andEqualTo("niceCount", comment.getNiceCount());
            }
            // 
            if (!StringUtils.isEmpty(comment.getHaha())) {
                criteria.andEqualTo("haha", comment.getHaha());
            }
        }
        return example;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        commentMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Comment
     *
     * @param comment
     */
    @Override
    public void update(Comment comment) {
        commentMapper.updateByPrimaryKey(comment);
    }

    /**
     * 增加Comment
     *
     * @param comment
     */
    @Override
    public void add(Comment comment) {
        commentMapper.insert(comment);
    }

    /**
     * 根据ID查询Comment
     *
     * @param id
     * @return
     */
    @Override
    public Comment findById(Long id) {
        return commentMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Comment全部数据
     *
     * @return
     */
    @Override
    public List<Comment> findAll() {
        return commentMapper.selectAll();
    }
}
